#include "audio_decoder.h"
#include "driver_gpio.h"
#include "adpcm-pcm.h"

extern ima_adpcm_state_t* state;
uint16_t task_id_audio_decode = TASK_ID_NONE;

Audio_Decode_t audio_decode = {0};

static uint8_t data[DECODE_AUDIO_FRAME_LEN] = {0};
static uint8_t decode_data[DECODE_AUDIO_FRAME_LEN*4] = {0};

 __attribute__((section("ram_code"))) int audio_decode_task(os_event_t *param)
{
	uint16_t wait_send_size = 0,wait_act_len = 0;
	uint16_t buffer_size = 0,act_len = 0;
	
    switch(param->event_id)
    {
			case DECODER_EVENT:
				
			audio_decode.status = APP_DECODER_STATE_BUSY;
			
			wait_act_len =  y_ringbuf_get_used_size(audio_decode.src_in_ringbuffer);
				
				if(wait_act_len>=(DECODE_AUDIO_FRAME_LEN))
				{
					wait_act_len = y_ringbuf_read_clear(audio_decode.src_in_ringbuffer,data,DECODE_AUDIO_FRAME_LEN);
					
			
				}
			
	
	
		//printf("1 heap size %d\r\n",os_get_free_heap_size());	

		//Encode frame
	
		int len = ima_adpcm_decode(state, (int16_t*)decode_data,(uint8_t*)data, wait_act_len/2);
		#include "driver_uart.h"
//		for(int i = 0;i<AUDIO_FRAME_LEN/4;i++)
//		{
//				printf("%02x",encode_data[i]);
//		}
//		
//		 printf("\r\n");
//src size %d,Encode data length is:%d\r\n",act_len, len);
	

		
		if(y_ringbuf_write(audio_decode.Decoder_out_ringbuffer,decode_data,len) == false)
		{
			printf("write decode data fail\r\n");
		}
		

			audio_decode.status = APP_DECODER_STATE_IDLE;
	
				
      break;
    }
    return EVT_CONSUMED;
}

void audio_decoder_init(void){

	audio_decode.src_in_ringbuffer		  = y_ringbuf_create(DECODE_RINGBUFFER_LEN);  
	audio_decode.Decoder_out_ringbuffer = y_ringbuf_create(DECODE_RINGBUFFER_LEN);  
	
	task_id_audio_decode = os_task_create(audio_decode_task);
	
}


__attribute__((section("ram_code"))) void msg_post_decode_handler()
{
	//printf("%s\r\n",__FUNCTION__);
	os_event_t event={0};
	event.event_id = DECODER_EVENT;
	event.src_task_id = TASK_ID_NONE;
	event.param = NULL;
	event.param_len = 0;
	os_msg_post(task_id_audio_decode,&event);
}


void msg_send_remain_decode_event()
{
							os_event_t event={0};
							event.event_id = DECODER_REMAIN_EVENT;
							event.src_task_id = TASK_ID_NONE;
							event.param = NULL;
							event.param_len = 0;
							os_msg_post(task_id_audio_decode,&event);
}




void audio_src_write_to_ringbuffer(uint8_t *data,int len)
{
	y_ringbuf_write(audio_decode.src_in_ringbuffer,data,len);
	
}

void audio_decode_write_to_ringbuffer(uint8_t *data,int len)
{
	y_ringbuf_write(audio_decode.Decoder_out_ringbuffer,data,len);
	
}



uint16_t audio_read_src_ringbuffer_len(uint8_t *read,int len)
{
	int read_len = y_ringbuf_read_clear(audio_decode.src_in_ringbuffer,read,len);
	return read_len;
}



uint16_t audio_read_decode_ringbuffer_len(uint8_t *read,int len)
{
	int read_len = y_ringbuf_read_clear(audio_decode.Decoder_out_ringbuffer,read,len);
	return read_len;
}

 __attribute__((section("ram_code"))) uint16_t get_audio_decoder_valid_size()
{
	return y_ringbuf_get_used_size(audio_decode.src_in_ringbuffer);
}



 __attribute__((section("ram_code"))) bool get_audio_decoder_is_busy(void)
{
		if(audio_decode.status == APP_DECODER_STATE_BUSY)
			return true;
		else
			return false;
}
